﻿using com.haoest.Utils.Net;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace com.haoest.QrPortal.FunctionPanels
{
    public sealed partial class ShareWifiPanel : BasePanel
    {
        public static readonly ShareWifiPanel Instance = new ShareWifiPanel();
        private string key = null;
        private string ssid = null;
        private string cmdResult = null;
        private Thread beginInvokeThread;
        private delegate void beginInvokeDelegate();

        private ShareWifiPanel()
        {
            InitializeComponent();
            qrCodeImgControl1.LightBrush = Brushes.Transparent;
        }

        public override void Reload()
        {
            panel_DevicesList.Controls.Clear();
            panel_DevicesList.Controls.Add(ConnectedDevicesList.Instance);
        }

        private void linkLabel_AutoGenerate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Random rd = new Random();
            ssid = "Portal" + rd.Next(1000, 9999);
            key = rd.Next(10000000, 99999999) + "";
            textBox_Ssid.Text = ssid;
            textBox_Key.Text = key;
        }

        private void button_Open_Click(object sender, EventArgs e)
        {
            ssid = textBox_Ssid.Text;
            key = textBox_Key.Text;
            Command.SaveSsid(ssid);
            Command.SaveKey(key);
            AddLog("正在启动虚拟Wifi...");
            beginInvokeThread = new Thread(new ThreadStart(OpenHostedNetwork));
            beginInvokeThread.Start();
        }


        private void button_Close_Click(object sender, EventArgs e)
        {
            AddLog("正在关闭虚拟Wifi...");
            beginInvokeThread = new Thread(new ThreadStart(CloseHostedNetwork));
            beginInvokeThread.Start();
        }

        private void ShareWifiPanel_Load(object sender, EventArgs e)
        {
            ssid = Command.GetSsid();
            key = Command.GetKey();
            textBox_Ssid.Text = ssid;
            textBox_Key.Text = key;
            GenerateQrCode(new string[] { ssid, key });
        }

        private void AddLog(string text)
        {
            textBox_Log.AppendText(DateTime.Now.ToString("[H:mm:ss] ") + text + "\n");
        }

        /// <summary>
        /// 开启承载网络
        /// </summary>
        private void OpenHostedNetwork()
        {
            cmdResult = NetworkManager.OpenHostedNetwork(ssid, key);
            // NetworkAdapter.EnableInternetConnectionSharing("无线网络连接", "虚拟Wifi");
            BeginInvoke(new beginInvokeDelegate(ShowHostedNetworkResult));
        }

        /// <summary>
        /// 关闭承载网络
        /// </summary>
        private void CloseHostedNetwork()
        {
            cmdResult = NetworkManager.CloseHostedNetwork();
            BeginInvoke(new beginInvokeDelegate(ShowHostedNetworkResult));
        }

        private void ShowHostedNetworkResult()
        {
            if (cmdResult.IndexOf("已启动承载网络") != -1)
            {
                string ipAddressString = NetworkManager.GetIPAddressByKeyword("Virtual").ToString();
                GenerateQrCode(new string[] { ssid, key, ipAddressString });
                AddLog("已生成虚拟Wifi, 网络名 (SSID): " + ssid + ", IP地址: " + ipAddressString);
            }

            else if (cmdResult.IndexOf("连到系统上的设备没有发挥作用") != -1)
            {
                AddLog("连到系统上的设备没有发挥作用");
            }

            else if (cmdResult.IndexOf("组或资源的状态不是执行请求操作的正确状态") != -1)
            {
                AddLog("组或资源的状态不是执行请求操作的正确状态");
            }

            else if (cmdResult.IndexOf("已停止承载网络") != -1)
            {
                AddLog("已关闭虚拟Wifi");
            }

            else
            {
                AddLog("未知状态");
            }
        }

        private void GenerateQrCode(string[] strings)
        {
            qrCodeImgControl1.Text = string.Join("@", strings);
        }
    }
}
